﻿using Microsoft.EntityFrameworkCore;
using nhCore.nMessage;
using System.Diagnostics;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace nhCore.db;

public class NSetting
{
    public const string con_主窗体标题 = "主窗体标题";
    public const string con_通讯方式 = "通讯方式";
    public const string con_通讯方式_com = "com";
    public const string con_通讯方式_tcp_server = "srv";

    public const string con_波特率 = "波特率";
    public const string con_停止位 = "停止位";
    public const string con_com连接超时 = "com连接超时";

    public const string con_Tcp服务器端口 = "Tcp服务器端口";

    public const string con_日起始时间 = "日起始时间";
    public const string con_保存间隔 = "保存间隔";

    public const string con_cgq = "cgq";

    public static readonly Dictionary<string, string> Dic种子数据 = 
        new(){
            {con_主窗体标题, "环境监测系统"},
            {con_通讯方式, con_通讯方式_tcp_server},
            {con_波特率, "9600"},
            {con_停止位, "1"},
            {con_com连接超时, "120"},
            {con_Tcp服务器端口, "7777"},
            {con_日起始时间, "0"},
            {con_保存间隔, "60"},
        };

    private Dictionary<string, string> KeyValues { get; set; } = [];

    public string ErrString { get; set; } = string.Empty;

    private NSetting()
    {
        try
        {
            using EnvDbContext envDbContext = new();

            foreach (FSetting fSetting in envDbContext.FSettings.AsNoTrackingWithIdentityResolution().ToList())
            {
                KeyValues.Add(fSetting.F_Key, fSetting.F_Value);
            }
        }
        catch (Exception ex)
        {
            ErrString = "无法连接到服务器。";
            Debug.WriteLine(ex);
        }
    }

    /// <summary>
    /// 根据Key读值
    /// </summary>
    /// <param name="key"></param>
    /// <returns>返回值，null为无键</returns>
    public string? ReadValue(string key)
    {
        _ = KeyValues.TryGetValue(key, out string? value);
        if (value == null)
        {
            NMessage.Error($"Key[{key}]不在数据库中。");
        }
        return value;
    }

    public void SetValue(string key, string value)
    {
        using EnvDbContext eimDbContext = new();

        if (KeyValues.TryAdd(key, value))
        {   //加入
            using EnvDbContext context = new();

            context.FSettings.Add(
                new FSetting(key, value));
            context.SaveChanges();
        }
        else
        {   //有Key则更新
            eimDbContext.FSettings.Where(s => s.F_Key == key).
            ExecuteUpdate(setters => setters.SetProperty(b => b.F_Value, value));
            KeyValues[key] = value;
        }
    }
    /// <summary>
    /// 是否是传感器直连
    /// </summary>
    public bool IsCgq { get => KeyValues[con_cgq] == "1"; }

    #region 单例模式
    private static readonly NSetting _dl = new();

    //显式的静态构造函数用来告诉C#编译器在其内容实例化之前不要标记其类型
    static NSetting() { }
    //声明一个私有的构造方法，让外部无法调用这个类的构造方法
    //private AccountData() { ReadDataBase(); }

    /// <summary>
    /// 返回对象单例
    /// </summary>
    public static NSetting Singleton { get => _dl; }
    #endregion
}
